//
//  HHMessageDetailViewController.m
//  HHBroker
//
//  Created by He Wei on 10/3/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHMessageDetailViewController.h"

@interface HHMessageDetailViewController ()

@end

@implementation HHMessageDetailViewController
@synthesize  myTableView, infoArray;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @" 消息详情";
    // Do any additional setup after loading the view.
    titleArray = @[@"姓名", @"线索id", @"添加时间", @"最近联系时间", @"自动丢弃时间"];
    [self requestData];
    [self InitCustomHeader];
    
    myTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 64+90, kCurrentWidth, kCurrentHeight-64-90) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    myTableView.dataSource = self;
    myTableView.sectionHeaderHeight = 20;
    [self.view addSubview:myTableView];

}

- (void)requestData
{
    __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlString = [NSString stringWithFormat:@"%@?action=%@", MESSAGE_DETIAL, self.urlAction];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        self.infoArray = [[NSMutableArray alloc] init];
        if (code == 1) {
            NSMutableArray *dataArray = [response objectForKey:@"data"];
            hub.mode = MBProgressHUDModeText;
            hub.labelText = NSLocalizedString(@"加载成功", @"");
            for (int i=1; i<dataArray.count; i++) {
                HHMessageDetailModel *model = [[HHMessageDetailModel alloc] init];
                model.cust_id = [dataArray[i] objectForKey:@"cust_id"];
                model.true_name = [dataArray[i] objectForKey:@"true_name"];
                model.assign_time = [dataArray[i] objectForKey:@"assign_time"];
                model.cv_create = [dataArray[i] objectForKey:@"cv_create"];
                model.lost_time = [dataArray[i] objectForKey:@"lost_time"];
                [self.infoArray addObject:model];
            }
            infoLabel.text = [response objectForKey:@"title"];
            [hub hide:YES afterDelay:1];
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = NSLocalizedString(@"加载失败", @"");
            [hub hide:YES afterDelay:2];
        
        
        }
        [myTableView reloadData];
    
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"加载失败", @"");
        [hub hide:YES afterDelay:2];
    
    }];
    

}

- (void)InitCustomHeader
{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 64, kCurrentWidth, 90);
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UIImageView *iconImage = [[UIImageView alloc] init];
    iconImage.image = self.nextImage;
    iconImage.frame = CGRectMake(15, 20, 50, 50);
    [view addSubview:iconImage];
    
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 15, kCurrentWidth/2-20, 30)];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.text = self.nextTitle;
    titleLabel.font = kJiaYuanFontWithSize(16);
    titleLabel.textColor = UIColorFromRGB(0x2d2d2d);
    [view addSubview:titleLabel];
    
    
    timerLabel = [[UILabel alloc]initWithFrame:CGRectMake(kCurrentWidth/2-20+80, 15, kCurrentWidth/4+30, 30)];
    timerLabel.textAlignment = NSTextAlignmentRight;
    timerLabel.text = self.nextTimer;
    timerLabel.font = kJiaYuanFontWithSize(12);
    timerLabel.textColor = UIColorFromRGB(0x5d5d5d);
    [view addSubview:timerLabel];
    
    
    infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 50,kCurrentWidth-90 , 30)];
    infoLabel.textAlignment = NSTextAlignmentLeft;
    infoLabel.font = kJiaYuanFontWithSize(12);
    infoLabel.numberOfLines = 2;
    infoLabel.textColor = UIColorFromRGB(0x5d5d5d);
    [view addSubview:infoLabel];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef cont = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(cont, UIColorFromRGB(0xb8b8b8).CGColor);
    CGContextSetLineWidth(cont, 1);
    CGFloat lengths[] = {2,2};
    CGContextSetLineDash(cont, 0, lengths, 2);  //画虚线
    CGContextBeginPath(cont);
    CGContextMoveToPoint(cont, 0.0, rect.size.height - 1);    //开始画线
    CGContextAddLineToPoint(cont, 320.0, rect.size.height - 1);
    CGContextStrokePath(cont);

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;//把高度设置很小，效果可以看成footer的高度等于0
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *strCell = @"msgCell";
    HHMessageDetailModel *model = infoArray[indexPath.section];
    NSArray *arr = @[model.true_name,model.cust_id,[NSString timeStampChinese:model.assign_time],[NSString timeStampChinese:model.cv_create],[NSString timeStampChinese:model.lost_time]];
    
    HHMessageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (cell == nil) {
        cell = [[HHMessageDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.valueLabel.text = arr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return infoArray.count;
}



@end
